<?php
	require("../wp-blog-header.php");
	require("event-config.php");

	if(!isset($_GET['p']))
    {
        header("Location: error.php");
    }
    else
    {
    	$event_id = $_GET['p'];
    	$query = "SELECT `name`, 
    	DATE_FORMAT(`start_date`,'%d-%m-%Y') AS `start_date`, 
    	DATE_FORMAT(`end_date`,'%d-%m-%Y') AS `end_date`
    	FROM $EVENT WHERE `event_id` = '$event_id'";

    	$result = first_row($query);
    	if($result){
    		$name = 	    $result['name'];
			$start_date =   $result["start_date"];
			$end_date =     $result["end_date"];
    	}
	    else{
	    	header("Location: error.php");
	    }
    }
?>

<html>
<head>
	<title><?php echo $name; ?></title>
	<meta charset='utf-8'/>
    <link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
	<header>
        <div class="header-wrapper">
            <img src="img/logo.png" class="header-logo" />
            <p class='header-title'><?php echo $name; ?></p>
        </div>
    </header>
	
    <div class='container'>
        <div class='post-wrapper'>
			<?php 
				echo "<h2>Danh sách tham gia \"$name\"</h2>";
				if($start_date != "00-00-0000")
					echo "<span class='post-detail'>Ngày bắt đầu: $start_date</span>";
				if($end_date != "00-00-0000")
					echo "<span class='post-detail'>Ngày kết thúc: $end_date</span>";

				$result = get_attendees_user($event_id);
				if($result->num_rows > 0)
				{
					//get poll
				    $poll = get_poll($event_id);
				    if ($poll != null) {
				    	$poll_title = "<th class='first-row'>Lựa chọn</th>";
				    }

					echo "<table class='list'>
							<tr>
								<th class='first-row left'>STT</th>
								<th class='first-row'>ID</th>
								<th class='first-row'>Họ tên</th>
								$poll_title
								<th class='first-row right'>Xác nhận</th>
							</tr>";
					$stt = 1;
					while ($row = next_row($result)) 
					{
						$user_info 	= get_userdata($row['id']);
						$username 	= $user_info->user_login;
					    $first_name = $user_info->user_firstname;
					    $last_name 	= $user_info->user_lastname;
					    $is_confirm = $row['is_confirm'];

					    //Get poll answer
					    if ($poll != null) {
					    	$answer = get_answers_by_user_and_event($username, $event_id);
					    	$poll_answer = "<th class='other-row'>$answer</th>";
					    }

					    if ($is_confirm) {
					    	$confirm = "<img src='img/v.png' />";
					    }
					    else {
					    	$confirm = "<img src='img/x.png' />";
					    }
					    echo "<tr>
							    <th class='other-row-center'>$stt</th>
							    <th class='other-row'>$username</th>
							    <th class='other-row'>$last_name $first_name</th>
							    $poll_answer
							    <th class='other-row-center'>$confirm</th>
					    	</tr>";
					    $stt++;
					}
					echo "</table>";
				}
				else
				{
					echo "<span class='post-detail'>Chưa có ai tham gia</span>";
				}
			 ?>
		</div>
	</div>
</body>
</html>